package com.ankang.art.service.impl;

import com.ankang.art.dto.HzsResultDto;
import com.ankang.art.mapper.HzsResultMapper;
import com.ankang.art.pto.HzsReultPto;
import com.ankang.art.service.HzsResultService;
import com.ankang.art.vo.Examination;
import com.ankang.art.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HzsResultServiceImpl implements HzsResultService {

    @Autowired
    HzsResultMapper hzsResultMapper;

    @Override
    public void deleteResult(Integer id) {
        hzsResultMapper.deleteResult(id);
    }

    @Override
    public List<HzsResultDto> getResult(Integer studentId, String studentName) {

        List<HzsResultDto> list = new ArrayList<>();

        List<Result> results = hzsResultMapper.getResult(studentId);

        for (int i=0 ;i <= results.size()-1 ;i++){

            HzsResultDto hzsResultDto = new HzsResultDto();

            if(null != results.get(i).getExamination_id()){
                Examination examination = hzsResultMapper.getExamination(results.get(i).getExamination_id());
                hzsResultDto.setBeginTime(examination.getBegin_time());
                hzsResultDto.setEndTime(examination.getEnd_time());




                if(null != results.get(i).getAvg()){
                    hzsResultDto.setAvg(results.get(i).getAvg());
                }else {
                    Integer num = examination.getTeacher_num();
                    Integer number = results.get(i).getNumber(results.get(i));
                    Integer max = results.get(i).getMax(results.get(i));
                    Integer min = results.get(i).getMin(results.get(i));
                    hzsResultDto.setAvg(Float.parseFloat(((number - max - min) / (num - 2)) + ""));
                    hzsResultMapper.saveAvg(hzsResultDto.getAvg(),results.get(i).getId());

                }
                hzsResultDto.setId(results.get(i).getId());
                hzsResultDto.setStudentName(results.get(i).getStudent_name());
                list.add(hzsResultDto);
            }
        }

        return list;
    }

    @Override
    public void updateResult(HzsReultPto hzsReultPto) {
        hzsResultMapper.updateResult(hzsReultPto);
    }
}
